// pages/HPStore/HPStore.js
const App = getApp();
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //商品状态筛选
    selectSptypes:'全部',
    //价格筛选
    selectPrice:'价格升序',
    //商品状态筛选列表
    sptypesList:[
      {datatypeId:'2', id: '0,1,3,4,5', name: '现货', checked: false },
      {datatypeId:'3',  id: '2', name: '预售', checked: false },
    ],
    //商品属性列表
    maintypesList:[
      { id: 0, name: '拼装模型', checked: false },
      { id: 2, name: '成品不可动手办', checked: false },
      { id: 15, name: '成品可动人偶模型', checked: false },
      { id: 3, name: '景品', checked: false },
      { id: 4, name: 'Q版人偶', checked: false },
      { id: 7, name: '毛绒', checked: false },
      { id: 5, name: '盒蛋', checked: false },
      { id: 6, name: '扭蛋', checked: false },
      { id: 8, name: '儿童玩具', checked: false },
      { id: 10, name: '立牌', checked: false },
      { id: 11, name: '其他周边产品', checked: false },
      // { id: 12, name: '虚拟商品', checked: false },
      // { id: 13, name: '其他类商品', checked: false },
    ],
    //详细筛选是否隐藏
    selectDetail:false,
    closeBool:true,
    minmoney:1,
    maxmoney:99999999,
    sort:'time',
    sptypes:'',
    datatype:'',
    key:'',
    maintypes:'',
    djsort:'time',
    priceType:0,
    hasMoreData:true,
    list:[],
    page:0,
    noContent:false//数据为空
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDataList();
  },
  //商品状态筛选
  changeSelectSptypes:function (){
    var that = this;
    wx.showActionSheet({
      itemList: ['全部', '现货', '预售'],
      success (res) {
        var selectSptypes;
        var sptypes;//商品状态
        var datatype;
        if (res.tapIndex == 0) {
          selectSptypes = '全部';
          sptypes = '';
          datatype = '2,3'
        }else if (res.tapIndex == 1){
          selectSptypes = '现货'
          sptypes = '0,1,3,4,5';
          datatype = '2'
        }else if (res.tapIndex == 2){
          selectSptypes = '预售'
          sptypes = 2
          datatype = '3'
        }

        that.setData({
          page:0,
          minmoney:1,
          maxmoney:99999999,
          selectSptypes:selectSptypes,
          sptypes:sptypes,
          datatype:datatype,
          list:[]
        })
        that.getDataList();
      },
      fail (res) {
        // console.log(res.errMsg)
      }
    })
  },
  //价格筛选
  changeSelectPrice:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['价格升序', '价格降序'],
      success (res) {
        var selectPrice;
        var sort;//排序
        if (res.tapIndex == 0) {
          selectPrice = '价格升序';
          sort = 'money01'
        }else if (res.tapIndex == 1){
          selectPrice = '价格降序'
          sort = 'money10'
        }

        that.setData({
          page:0,
          minmoney:1,
          maxmoney:99999999,
          selectPrice:selectPrice,
          djsort:sort,
          list:[]
        })
        that.getDataList();
      },
      fail (res) {
        // console.log(res.errMsg)
      }
    })
  },
  //最新排序
  changeSelectTime:function (){
    var that = this;
    that.setData({
      page:0,
      djsort:'time',
      list:[]
    })
    that.getDataList();
  },
  //详细筛选
  changeSelectDetail:function () {
    var that = this;
    that.setData({
      page:0,
      selectDetail:true
    })
  },
  //商品类型筛选
  sptypesListSelect:function (e) {
    let index = e.currentTarget.dataset.index;
    let sptypesList = this.data.sptypesList;
    let newli = 'sptypesList[' + index + '].checked';
    this.setData({
      [newli]: !this.data.sptypesList[index].checked
    })
  },
  // //加密筛选
  // filterList:function(e){
  //   let index = e.currentTarget.dataset.index;
  //   let filterList = this.data.filterList;
  //   let newli = 'filterList[' + index + '].checked';
  //   this.setData({
  //     [newli]: !this.data.filterList[index].checked
  //   })
  // },
  //商品属性筛选
  maintypesListSelect:function (e) {
    let index = e.currentTarget.dataset.index;
    let maintypesList = this.data.maintypesList;
    let newli = 'maintypesList[' + index + '].checked';
    this.setData({
      [newli]: !this.data.maintypesList[index].checked
    })
  },
  //确定筛选
  confirmSelect:function (e){
    let sptypesList = this.data.sptypesList;
    let maintypesList = this.data.maintypesList;
    let filterList = this.data.filterList;
    let minmoney = this.data.minmoney;
    let maxmoney = this.data.maxmoney;
    // console.log(maxmoney)
    if (minmoney > maxmoney) {
      wx.showToast({
        title: '最低价不能大于最高价',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let sptypesListId = [];
    let datatypeId = [];
    let maintypesListId = [];
    for (let i = 0; i < sptypesList.length; i++) {
      if (sptypesList[i].checked) {
        sptypesListId.push(sptypesList[i].id);
        datatypeId.push(sptypesList[i].datatypeId);
      }
    }
    for (let i = 0; i < maintypesList.length; i++) {
      if (maintypesList[i].checked) {
        maintypesListId.push(maintypesList[i].id);
      }
    }
    // if (filterList[0].checked) {
    //   this.setData({
    //     filter:0
    //   })
    // }else{
    //   this.setData({
    //     filter:1
    //   })
    // }
    let sptypes = sptypesListId.toString(',');
    let datatypeIdStr = datatypeId.toString(',');
    let maintypes = maintypesListId.toString(',');
    this.setData({
      page:0,
      sptypes:sptypes,
      datatype:datatypeIdStr,
      selectDetail:false,
      maintypes:maintypes,
      list:[]
    })
    this.getDataList();
  },
  //重置筛选
  resetSelect:function () {
    let sptypesList = this.data.sptypesList;
    let maintypesList = this.data.maintypesList;
    for (let i = 0; i < sptypesList.length; i++) {
      sptypesList[i].checked = false
    }
    for (let i = 0; i < maintypesList.length; i++) {
      maintypesList[i].checked = false
    }
    this.setData({
      sptypesList:sptypesList,
      maintypesList:maintypesList,
      minmoney:1,
      datatype:'2,3',
      maxmoney:99999999,
    })
    // this.saltedEggList();
  },
  //获取最低价
  getMinmoneyInput:function(e){//方法1
		this.setData({
      minmoney: e.detail.value
    })
  },
   //获取最高价
   getMaxmoneyInput:function(e){//方法1
    let maxMoney = (e.detail.value) * 100
		this.setData({
      maxmoney: maxMoney
    })
  },
  //详细筛选隐藏
  hidden:function () {
    this.setData({
      selectDetail:false
    })
  },
  //搜索离焦
  bindblur:function(e){
    var discountName=e.detail.value['search - input'] ?e.detail.value['search - input'] : e.detail.value 
    console.log(discountName);
    this.setData({
      page:0,
      key: discountName,
      list:[]
    })
    this.getDataList();
  },
  bindinput:function(e){
    var val = e.detail.value;
    var closeBool;
    if (val == '') {
      closeBool = true
    }else{
      closeBool = false
    }
    this.setData({
      closeBool:closeBool
    })
  },
  closeSearch:function (){
    this.setData({
      closeBool:true,
      page:0,
      key: '',
      list:[]
    })
    this.getDataList();
  },
  getDataList:function (){
    var that = this;
    let page = that.data.page;
    let key = that.data.key;
    let djsort = that.data.djsort;
    let sptypes = that.data.sptypes;
    let minmoney = that.data.minmoney;
    let maxmoney = that.data.maxmoney;
    let datatype = that.data.datatype;
    let maintypes = that.data.maintypes;
    utils.requestUrl({
      url:'/niu/apiv3/niu_getlisthpv5.php',
      params:{
        page:page,
        rowcount:30,
        sptypes:sptypes,
        datatype:datatype,
        markid:wx.getStorageSync('markId'),
        key:key,
        djsort:djsort,
        minmoney:minmoney,
        maxmoney:maxmoney,
        maintypes:maintypes
      }
    }).then((res) =>{
      console.log(res.data)
      var contentListTem = that.data.list;
      if (res.data.datacount == 0 || res.data.datacount == null) {
        that.setData({
          noContent:true
        })
      }else{
        that.setData({
          noContent:false
        })
      }
      if (res.data.datacount > 0) {
        var contentList = res.data.data
        

        if (contentList.length < res.data.pagesize) {
          that.setData({
            list:contentListTem.concat(contentList),
            hasMoreData: false
          })
        }else{
          that.setData({
            list: contentListTem.concat(contentList),
            hasMoreData: true,
            page: page + 1
        })
        }
      }
    })
  },
  goToDetail:function (e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/HPStore/HPStoreDetail/HPStoreDetail?id='+id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:0,
      list:[]
    })
    this.getDataList()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      // console.log('触底了')
      this.getDataList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})